﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WEBDT.Base;
using WEBDT.ViewModel;
namespace WEBDT.Areas.Admin.Controllers
{
    public class TinTucController : BaseController<TinTuc>
    {
        //
        // GET: /Admin/TinTuc/

        public ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        public override ActionResult insert()
        {
            var model = new TintucModel
            {
                tontai = false,
            };
            ViewData["maloai"] = new SelectList(repository.Danhsach<LoaiTT>(), "maloai", "tenloai");
            return View(model);
        }

        [HttpPost]
        public override ActionResult insert(FormCollection fr)
        {
            var ht = new TintucModel
            {
                TieuDe = fr.Get("tieude"),
                NoiDung = fr.Get("noidung"),
                NgayGui = fr.Get("ngaygui"),
                NguoiGui = fr.Get("nguoigui"),
                MaLoai = Convert.ToInt32(fr.Get("maloai")),
            };
            var model = new TinTuc
            {
                TieuDe = ht.TieuDe,
                NoiDung= ht.NoiDung,
                NgayGui= ht.NgayGui,
                NguoiGui= ht.NguoiGui,
                MaLoai = ht.MaLoai,
            };
            repository.Insert<TinTuc>(model);
            return RedirectToAction("getAll");
        }

        [HttpGet]
        public override ActionResult update(int id)
        {
            var model = repository.SelectByID<TinTuc>(id);
            var ht = new TintucModel
            {
                TieuDe= model.TieuDe,
                NoiDung= model.NoiDung,
                NguoiGui= model.NguoiGui,
                NgayGui= model.NgayGui,
                MaLoai = model.MaLoai,
            };
            ViewData["maloai"] = new SelectList(repository.Danhsach<LoaiTT>(), "maloai", "tenloai");
            return View(ht);
        }

        [HttpPost]
        public override ActionResult update(FormCollection fr)
        {
            var ht = new TintucModel
            {
                Matintuc = Convert.ToInt32(fr.Get("matintuc")),
                TieuDe = fr.Get("tieude"),
                NoiDung = fr.Get("noidung"),
                NgayGui = fr.Get("ngaygui"),
                NguoiGui = fr.Get("nguoigui"),
                MaLoai = Convert.ToInt32(fr.Get("loaitintuc")),
            };
            var model = repository.SelectByID<TinTuc>(ht.Matintuc);
            model.TieuDe = ht.TieuDe;
            model.NoiDung = ht.NoiDung;
            model.NguoiGui = ht.NguoiGui;
            model.NgayGui = ht.NgayGui;
            model.MaLoai = ht.MaLoai;
            repository.commitchanges();
            return RedirectToAction("getAll");
        }
    }
}
